#include "com.h"

int main()
{
  if(mkfifo(PATHNAME, 0644) < 0){
    perror("mkfifo");
    return 1;
  } 

  int fd = open(PATHNAME, O_RDONLY);
  if(fd < 0){
    perror("open");
    return 1;
  }
  
  char msg[128];
  while(1){
    sleep(1);
    ssize_t s = read(fd, msg, sizeof(msg) - 1);
    if(s < 0){
      perror("read");
      break;;
    }
    else if(s == 0){
      printf("read file end\n");
      break;
    }
    else{
      msg[s] = 0;
      printf("client# %s\n", msg);
      char* myargv[32] = {0};
      myargv[0] = strtok(msg, " ");
      int i = 1;
      while(myargv[i] = strtok(NULL, " ")) i++;

      pid_t id = fork();
      if(id == 0){
        execvp(msg, myargv);
        exit(0);
      }

      waitpid(-1, NULL, 0);
    }
  }
  return 0;
} 
